package com.shop.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextListener implements ServletContextListener {
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		
		ServletContext application = event.getServletContext();
		String configLocation = application.getInitParameter("contextConfigLocation");
		System.out.println("加载的文件名：------------------"+configLocation);
		//加载spring容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		//1.通过初始化参数的方式获取spring配置文件 applicationContext.xml,需要在web.xml中配置文件参数
		
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.直接是配置文件
		application.setAttribute("ctx", ctx);
		
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		System.out.println("---监听器在结束---");
		
	}

	

}
